<?php
namespace app\admin\model;
use think\Model;
use app\admin\model\Areas;
use app\admin\model\BaseModel;
class Clouds extends BaseModel
{
	/*获取云列表*/
	public function getClouds($pid=0){
		$result = self::alias('m')
			->join('cloud_items a',"a.cloud_id=m.id")
			->where(array('m.status'=>1))->order('m.name'.$this->prexLang().' asc,sort asc')->field('m.id,m.name'.$this->prexLang().' as name ,m.country_id,m.icon')->group('m.id')->select();
			return $result;
	}
	/*获取云列表*/
	public function getCloudsList($condition=[]){
		$result = self::alias('m')
			->where($map)->order('m.name'.$this->prexLang().' asc,sort asc')->field('m.id,m.name'.$this->prexLang().' as name ,m.country_id,m.icon,m.status,m.sort,m.icon')->select();
			return $result;
	}
	public static function getName($id){
		$name = self::where('id',$id)->value('name'.self::prexLang());
		return $name ? lang($name) : '--';
	}
	public static function getNameAttr($valu,$data){
		return $valu ? $valu : '--';
	}
	public function getNamel($valu,$data){

		return isset($data['name'.$this->prexLang()]) ? $data['name'.$this->prexLang()] : $valu;
	}
	public static function getNameLabel($id){
		$name = self::where(['id'=>$id])->value('name'.self::prexLang());
		return $name ? lang($name) : '--';
	}
	public static function getCountry($cloud_id,$cloud_ids=0){
		$countrys = [];
		$countryids = db('cloud_items')->where(['cloud_id'=>$cloud_id,'status'=>1])->column('id,country_id','country_id');
		if($countryids){
			$areas = new Areas;
			$countrys = $areas->where('id',"in",array_keys($countryids))->field('id,name'.self::prexLang().' as name,lng,lat,icon')->order('name'.self::prexLang().' asc,sort asc')->select();
		}
		return $countrys;
	}	
	public static function getCity($cloud_id,$country_id){
		$citys = [];
		$cityIds = db('cloud_items')->where(['country_id'=>$country_id,'cloud_id'=>$cloud_id,'status'=>1])->column('id,city_id','city_id');
		if($cityIds){
			$areas = new Areas;
			$citys = $areas->where('id',"in",array_keys($cityIds))->where(['pid'=>$country_id])->field('id,name'.self::prexLang().' as name,lng,lat,icon')->order('name'.self::prexLang().' asc,sort asc')->select();
		}
		return $citys;
	}
	public static function getItems($cloud_id,$country_id,$city_id){
		$items = db('cloud_items')->where(['country_id'=>(int)$country_id,'cloud_id'=>(int)$cloud_id,'city_id'=>(int)$city_id,'status'=>1])->field('id,name')->select();
		
		return $items;
	}
}